package com.wanqing.community.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

/**
 * 
 * <p>Description：</p>
 */
public class MD5 {
	
	private static Logger log = Logger.getLogger(MD5.class);

	/**
	 * <p>Description:MD5加密<p>
	 * <p>modifiContent:<p>
	 * @param source
	 * @return
	 */
	public static String md5Encryption(String source){
		if(StringTools.isEmptyOrNull(source)){
			return null;
		}
		char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
		try {
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(source.getBytes());
			byte[] bytes = mdInst.digest();
			int j = bytes.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = bytes[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
		} catch (NoSuchAlgorithmException e) {
			log.error(""+e.toString());
		}
		return null;
	}
}
